class COLLISION_MANAGE{
	//碰撞数据
    private judge:CBASE[]=[] //碰撞成员
    private static myself:COLLISION_MANAGE
	private constructor() {
       
	}
    public static get getCOLLISIONCONTRL():COLLISION_MANAGE
    {
        if(this.myself==undefined)this.myself=new COLLISION_MANAGE;
        return this.myself
    } 
	 public ADDjudge(a:CBASE)//添加碰撞到自动发生碰撞
    {
     
      for(var b=0;b<this.judge.length;b++) //筛选已添加的
      {
          if(this.judge[b]==a)
          {
              break;
          }
      }
      if(b==this.judge.length)
      {
        this.judge.push(a)
      }
      
    }
  
    public removejudge(a:CBASE):boolean //删除自动发生碰撞
    {
       for(let b=0;b<this.judge.length;b++)
       {
           if(this.judge[b]==a)
           {
                this.judge.splice(b,1)
                return true;
           }
       }
       return false;
    }
    public ALL_collision() //发生碰撞（帧更新）
    {
       
        for(var a=0;a<this.judge.length-1;a++)
        {
            for(var b=a+1;b<this.judge.length;b++)
               
             this.Collision(this.judge[a],this.judge[b])
        }
    }
 
    private Collision(obja:CBASE,objb:CBASE) //每个对象内的每个碰撞盒发生碰撞
    {

        
       	for(let a=0;a<obja.judge.length;a++)
		{
			for(let b=0;b<objb.judge.length;b++)
			{    
                
                 let objaprv=obja.judge[a].judge
                 let objbprv=objb.judge[b].judge
				if(obja.judge[a].type!=judgetype.circle&&objb.judge[b].type!=judgetype.circle)
				{
					 obja.judge[a].collision_twoshape(objb.judge[b])
				}
				else if((obja.judge[a].type==judgetype.circle||objb.judge[b].type==judgetype.circle)&&obja.judge[a].type!=objb.judge[b].type)
				{
					 obja.judge[a].collision_onecircle(objb.judge[b])
				}
				else 
				{
					 obja.judge[a].collision_twocircle(objb.judge[b])
				}
                let objanow=obja.judge[a].judge
                let objbnow=objb.judge[b].judge

                let massagea=this.sendmassage(objaprv,objanow)
                let massageb=this.sendmassage(objbprv,objbnow)
                if(massagea)obja.sendevent(massagea,objb,objb.judge[b].name,obja.judge[a].name); //发送碰撞类型及该碰撞盒的名字
                if(massageb)objb.sendevent(massageb,obja,obja.judge[a].name,objb.judge[b].name); //发送碰撞类型及该碰撞盒的名字
			}
		}
    }
    private sendmassage(prv:boolean,now:boolean) //检查结果属于开始碰撞还是结束碰撞还是正在碰撞
    {
        if(prv==false&&prv==now)
        {
            return false
        }
        else if(prv==false&&now==true)
        {
            return events.beginjudge
        }
        else if(prv==true&&now==false)
        {
            return events.endjudge
        }
        else
        {
            return events.combojudge
        }
    }

}